    #include <QApplication>
#include <QWidget>
#include <QRectF>
#include <QPainter>
#include <QVector>
#include <QPolygonF>
#include <cmath>
#include <QPointF>
#include <cmath>

#include <QDebug>
#ifndef POLYGONWIDGET_H
#define POLYGONWIDGET_H
#include "algraph.h"
class PolygonWidget : public QWidget {
public:
    PolygonWidget(QWidget *parent = nullptr,ALGraph<int> *alg=nullptr) : QWidget(parent), alg(alg),sides(5), radius(100), centerX(150), centerY(150) {}
    void SetALGraph(ALGraph<int> *alg){
        this->alg=alg;
        //this->sides= this->alg->vexnum;
        update();
    }
    void setPolygon(int sides, int radius, int centerX, int centerY) {
        //this->sides = sides;
        this->radius = radius;
        this->centerX = centerX;
        this->centerY = centerY;
        update(); // Trigger a repaint
    }

protected:

    void paintEvent(QPaintEvent *event) override {
        maps.clear();
        QPainter painter(this);
        painter.setFont(QFont("Arial",20));
        painter.setRenderHint(QPainter::Antialiasing);

        // Calculate polygon vertices
        QPolygonF polygon;
        //qDebug()<<"sides="<<alg->vexnum;
        for (int i = 0; i < alg->vexnum; ++i) {
            //qDebug()<<"currentnode"<<currentnode;
            if(currentnode==i){
                painter.setPen(QPen(Qt::yellow));
            }else{
                painter.setPen(QPen(QColor(0,0,0)));
            }
            double angle = 2 * M_PI * i / alg->vexnum;
            double x = centerX + radius * cos(angle);
            double y = centerY + radius * sin(angle);
            polygon.append(QPointF(x, y));
            maps.append(QPointF(x+50,y+50));


            //qDebug()<<alg->vertices[i].data;
            painter.drawText((x+50-20),y+50+10,QString::number( alg->vertices[i].data));
            painter.drawEllipse(QRectF(x,y,100,100));

            //qDebug() << "Vertex" << i << ":" << x << "," << y;
        }
        painter.setPen(QPen(QColor(0,0,0)));
        for(int i=0;i<alg->vexnum;i++){
            auto temp=alg->vertices[i].firstarc;
            while(temp){
                auto frontpoint=maps[i];
                auto topoint=maps[temp->adjvex];
                //auto middlex=(frontpoint.x()+topoint.x())/2;
                //auto middley=(frontpoint.y()+topoint.y())/2;
                auto beginlinep=calculateIntersection(frontpoint,50,frontpoint,topoint);
                auto endlinep=calculateIntersection(topoint,50,frontpoint,topoint);

                QPointF *blp,*elp;
                if(frontpoint.x()>=centerX){

                    blp=&((beginlinep[0].x()>beginlinep[1].x())?(beginlinep[1]):(beginlinep[0]));
                }else{
                   blp=&((beginlinep[0].x()>beginlinep[1].x())?(beginlinep[0]):(beginlinep[1]));
                }
                if(topoint.x()>=centerX){
                     elp=&((endlinep[0].x()>endlinep[1].x())?(endlinep[1]):(endlinep[0]));
                }else{
                     elp=&((endlinep[0].x()>endlinep[1].x())?(endlinep[0]):(endlinep[1]));
                }
                painter.drawLine(*blp,*elp);

                QPointF vector = *elp - *blp;
                double length = sqrt(vector.x() * vector.x() + vector.y() * vector.y());
                vector.setX(vector.x() / length);
                vector.setY(vector.y() / length);
                double arrowHeadAngle = 20.0;
                double arrowHeadLength = 20.0;

                QPointF arrowBase = *elp - vector * arrowHeadLength / 2.0;
                QPointF arrowSide1 = arrowBase + QPointF(-vector.y() * tan(qDegreesToRadians(arrowHeadAngle)) * arrowHeadLength,
                                                         vector.x() * tan(qDegreesToRadians(arrowHeadAngle)) * arrowHeadLength);
                QPointF arrowSide2 = arrowBase + QPointF(vector.y() * tan(qDegreesToRadians(arrowHeadAngle)) * arrowHeadLength,
                                                         -vector.x() * tan(qDegreesToRadians(arrowHeadAngle)) * arrowHeadLength);

                // 画箭头头部
                QPolygonF arrowHead(3);
                arrowHead[0] = *elp;
                arrowHead[1] = arrowSide1;
                arrowHead[2] = arrowSide2;
                painter.setBrush(Qt::black);
                painter.drawPolygon(arrowHead);

                temp=temp->next;

            }
        }
        // Draw polygon
        painter.setPen(Qt::black);
        //painter.drawPolygon(polygon);
    }
    QVector<QPointF>calculateIntersection(const QPointF &center, qreal radius, const QPointF &p1, const QPointF &p2) {
        QVector<QPointF> intersections;


        qreal dx = p2.x() - p1.x();
        qreal dy = p2.y() - p1.y();
        qreal a = dx * dx + dy * dy;
        qreal b = 2 * (dx * (p1.x() - center.x()) + dy * (p1.y() - center.y()));
        qreal c = (p1.x() - center.x()) * (p1.x() - center.x()) + (p1.y() - center.y()) * (p1.y() - center.y()) - radius * radius;

        qreal discriminant = b * b - 4 * a * c;

        if (discriminant >= 0) {

            qreal t1 = (-b - std::sqrt(discriminant)) / (2 * a);
            qreal t2 = (-b + std::sqrt(discriminant)) / (2 * a);


            QPointF intersection1 = p1 + t1 * QPointF(dx, dy);
            QPointF intersection2 = p1 + t2 * QPointF(dx, dy);

            intersections.append(intersection1);
            if (t1 != t2) {
                intersections.append(intersection2);
            }
        }

        return intersections;
    }
    double qDegreesToRadians(double degrees) {
        return degrees * M_PI / 180.0;
    }
public:
    int sides;//废弃不用
    int radius;
    QVector<QPointF> maps;
    int centerX;
    int centerY;
    int currentnode=0;
    ALGraph<int> *alg;
};

#endif // POLYGONWIDGET_H
